<template>
    <div class="widgets bg-white">
        <el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
            <el-button type="info" @click="refresh">刷新</el-button>
            <el-button type="primary" v-if="isAdd" @click="add">新增</el-button>
            <el-button type="primary" v-else disabled>新增</el-button>
            <el-button type="danger" v-if="!isDel" disabled>删除</el-button>
            <el-button type="danger" v-else @click="del">删除</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
            <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
            <el-button type="success" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
            <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
            <el-button-group class="btn-other">
                <el-button v-if="multipleSelection.length <= 0" disabled>
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导出" v-else @click="exportExcel">
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导入">
                    <el-upload class="upload-demo" :action="$url" :show-file-list="false" multiple style="display: inline-block;">
                        <img src="../../../assets/images/import.png" alt="">
                    </el-upload>
                </el-button>                        
                <el-button title="打印">
                    <img src="../../../assets/images/print.png" alt="">
                </el-button>
                <el-button title="帮助">
                    <img src="../../../assets/images/help.png" alt="">
                </el-button>
            </el-button-group>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            searchText: '',
            multipleSelection: this.$store.state.auxiliaryStore.multipleSelection,
            selectRowId: this.$store.state.auxiliaryStore.selectRowId,
            row: this.$store.state.auxiliaryStore.row,
            isDel: false,
            isAdd: true
        }
    },
    watch: {
        multipleSelection(newVal) {
            if(this.row[0]['level'] === 0) {          //监听选中表格数据变化，如果小类，每次只能删除一条数据
                newVal.length != 1 ? this.isDel = false : this.isDel = true;
            } else if(this.row[0]['level'] === 1) {
                newVal.length <= 0 ? this.isDel = false : this.isDel = true;
            }
        },
        row(newVal) {
            let activeName = this.$store.state.auxiliaryStore.activeName;
            if(activeName[0] === 'first') {
                newVal[0]['level'] === 0 ? this.isAdd = true : this.isAdd = false;
            } else if(activeName[0] === 'second') {
                this.isAdd = true;
            }
        }
    },
    methods: {
        search() {

        },
        refresh() {
            const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
        },
        add() {
            this.$store.commit('auxiliaryStore/updateRowId');           //新增之前先把rowId清空，防止是修改状态
            let activeName = this.$store.state.auxiliaryStore.activeName;
            if(activeName[0] === 'first') {
                this.$util.openLayerIframe({title: '小类-新增', zIndex: 13, width: '500px', height: '360px', url: this.$domain + '/#/basicData/auxiliary/subClassForm'});
            } else if(activeName[0] === 'second') {
                if(this.row[0]['level'] === 0) {
                    this.$util.openLayerIframe({title: '小类-新增', zIndex: 13, width: '500px', height: '360px', url: this.$domain + '/#/basicData/auxiliary/subClassForm'});
                } else if(this.row[0]['level'] === 1) {
                    switch(this.row[0]['dataCategoryId']) {
                        case 0:
                            this.$util.openLayerIframe({title: '车牌号码-新增', zIndex: 13, width: '500px', height: '410px', url: this.$domain + '/#/basicData/auxiliary/carNumberForm'});
                            break;
                        case 1:
                            this.$util.openLayerIframe({title: '合同编号-新增', zIndex: 13, width: '500px', height: '420px', url: this.$domain + '/#/basicData/auxiliary/contractNumberForm'});
                            break;
                        case 2:
                            this.$util.openLayerIframe({title: '货号资料-新增', zIndex: 13, width: '500px', height: '380px', url: this.$domain + '/#/basicData/auxiliary/goodsInfoForm'});
                            break;
                        case 3:
                            this.$util.openLayerIframe({title: '普通资料-新增', zIndex: 13, width: '500px', height: '300px', url: this.$domain + '/#/basicData/auxiliary/ordinaryForm'});
                            break;
                        case 4:
                            this.$util.openLayerIframe({title: '银行资料-新增', zIndex: 13, width: '500px', height: '415px', url: this.$domain + '/#/basicData/auxiliary/bankForm'});
                            break;
                        default: break;
                    }
                }
            }
        },
        del() {
            if(this.row[0]['level'] === 0) {
                //删除小类
                this.$confirm('确定删除该小类', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(res => {
                    this.$http.delete('auxiliary/delete', {data: this.selectRowId}).then(res => {
                        if(res.data.code == 20001) {
                            this.$message({message: '删除小类成功', type: 'success', duration: 1500});
                            setTimeout(() => {
                                this.$store.dispatch('auxiliaryStore/getTreeData');
                            }, 1500);
                        }
                    });
                }).catch(() => {
                    this.$message({message: '已取消删除', type: 'info', duration: 1500});
                });
            } else {
                //删除小类明细
                this.$confirm('确定删除选中数据', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(res => {
                    let $url = null;
                    switch(this.row[0]['dataCategoryId']) {
                        case 0:
                            $url = 'auxiliaryInfoAllCategory/deleteCarNumber';
                            break;
                        case 1:
                            $url = 'auxiliaryInfoAllCategory/deleteContractNumber';
                            break;
                        case 2:
                            $url = 'auxiliaryInfoAllCategory/deleteGoodsInfo';
                            break;
                        case 3:
                            $url = 'auxiliaryInfoAllCategory/deleteOrdinary';
                            break;
                        case 4:
                            $url = 'auxiliaryInfoAllCategory/deleteBank';
                            break;
                        default: break;
                    }
                    this.$http.delete($url, {data: this.selectRowId}).then(res => {   //这里有右键单条删除和普通多条删除
                        if(res.data.code == 20001) {
                            this.$message({message: '删除成功', type: 'success', duration: 1500});
                            setTimeout(() => {
                                this.$store.dispatch('auxiliaryStore/getSubClassTableData');
                            }, 1000);
                        } else {
                            this.$message({message: res.data.msg || '删除失败', type: 'error', duration: 1500});
                        }
                    });
                });
            }
        },
        disabedBtn(status, msg) {
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['state'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            if(this.row[0]['level'] === 0) {
                //禁用小类
                this.$http.post('auxiliary/updateState?state=' + status, JSON.stringify(this.selectRowId), {
                    headers: {'Content-type': 'application/json;charset=UTF-8'}
                }).then(res => {
                    if(res.data.code == 20001) {
                        this.$message({message: msg + '成功', type: 'success', duration: 2000});
                        setTimeout(() => {
                            this.$store.dispatch('auxiliaryStore/getTreeData');
                        }, 1000);
                    } else {
                        this.$message({message: res.data.msg || msg + '失败', type: 'error', duration: 1500});
                    }
                });
            } else if(this.row[0]['level'] === 1) {
                //禁用明细
                let $url = null;
                switch(row['dataCategoryId']) {         //配置各个大类下面的小类禁用、启用api
                    case 0:
                        $url = 'auxiliaryInfoAllCategory/updateCarNumberState?state=' + status;
                        break;
                    case 1:
                        $url = 'auxiliaryInfoAllCategory/updateContractNumberState?state=' + status;
                        break;
                    case 2:
                        $url = 'auxiliaryInfoAllCategory/updateGoodsInfoState?state=' + status;
                        break;
                    case 3:
                        $url = 'auxiliaryInfoAllCategory/updateOrdinaryState?state=' + status;
                        break;
                    case 4:
                        $url = 'auxiliaryInfoAllCategory/updateBankState?state=' + status;
                        break;
                    default: break;
                }
                this.$http.post($url, JSON.stringify(this.selectRowId), type).then(res => {
                    if(res.data.code == 20001) {
                        this.$message({message: msg + '成功', type: 'success', duration: 1500});
                        setTimeout(() => {
                            this.$store.dispatch('auxiliaryStore/getSubClassTableData');           //刷新数据
                        }, 1000);
                    } else {
                        this.$message({message: res.data.msg || msg + '失败', type: 'error', duration: 1500});
                    }
                });
            }
        },
        exportExcel() {
            if(this.row[0]['level'] === 0) {        //小类导出
                let idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
                this.$http.get('auxiliary/export',{params: {idStrs: idSting}}).then(res => {
                    this.$message({message: '导出成功', type: 'success', duration: 1500});
                    window.location.href = this.$url + 'auxiliary/export?idStrs=' + idSting;          //导出时触发
                }).catch(err => {
                    this.$message.error(err);
                });
            } else if(this.row[0]['level'] === 1) {
                let idSting = this.selectRowId.join(','),
                    $url = null;
                switch(this.row[0]['dataCategoryId']) {
                    case 0:
                        $url = 'auxiliaryInfoAllCategory/exportCarNumber';
                        break;
                    case 1:
                        $url = 'auxiliaryInfoAllCategory/exportContractNumber';
                        break;
                    case 2:
                        $url = 'auxiliaryInfoAllCategory/exportGoodsInfo';
                        break;
                    case 3:
                        $url = 'auxiliaryInfoAllCategory/exportOrdinary';
                        break;
                    case 4:
                        $url = 'auxiliaryInfoAllCategory/exportBank';
                        break;
                    default: break;
                }
                this.$http.get($url, {params: {idStrs: idSting}}).then(res => {
                    this.$message({message: '导出成功', type: 'success', duration: 1500});
                    window.location.href = this.$url + ''+ $url +'?idStrs=' + idSting;          //导出时触发
                }).catch(err => {
                    this.$message.error(err);
                });
            }
        }
    }
}
</script>